package com.ruyuan.dependency.product.service.impl;

import com.ruyuan.dependency.product.entity.Sku;
import com.ruyuan.dependency.product.mapper.SkuMapper;
import com.ruyuan.dependency.product.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zhonghuashishan
 */
@Service
public class SkuServiceImpl implements SkuService {

    @Autowired
    private SkuMapper skuMapper;

    @Override
    public Sku queryById(Long skuId) {
        return skuMapper.selectById(skuId);
    }

    @Override
    public List<Sku> queryByIds(List<Long> skuIds) {
        return skuMapper.selectBatchIds(skuIds);
    }

    @Override
    public boolean lockStock(Long skuId, Integer count) {
        int affectedRows = skuMapper.lockStock(skuId, count);
        return affectedRows == 1;
    }

    @Override
    public boolean releaseStock(Long skuId, Integer count) {
        int affectedRows = skuMapper.releaseStock(skuId, count);
        return affectedRows == 1;
    }

    @Override
    public void save(Sku sku) {
        sku.setCreateTime(new Date());
        sku.setUpdateTime(sku.getCreateTime());
        skuMapper.insert(sku);
    }
}